<?php
class Ssopanel_UserController extends Ext_Controller_UserController
{
 
    public function loginAction()
    {
    	$auth = Zend_Auth::getInstance();
		if ( $auth->hasIdentity() ) {
			return $this->_redirect('/SsoPanel/');
		}
    	
        $this->view->headLink()->appendStylesheet( '/css/login.css' );
        // try to obtain the organization or owner for this
        // session
        $ownerId = null;
        if ( $this->_session->ownerId ) {
            $ownerId = $this->_session->ownerId;
        }
        
        // Owner can be overiden if explicitely set
        if ( $this->_request->getParam( 'ownerId' ) ) {
            $ownerId = $this->_request->getParam( 'ownerId' );
            // save it in session
            $this->_session->ownerId = $ownerId;
        }
        
        // In case no organization was specified use the
        // default from the config file.
        if ( $ownerId === null ) {
            $ownerId = $this->_config->user->login->ownerId;
        }

        $userForm = new Ext_Forms_Authentication();
        // handle login
        
        if ( $this->getRequest()->isPost() ) {
			if ( $userForm->isValid( $this->getRequest()->getPost() ) ) {

				$loginStatus = new SsoPanel_Model_UserLoginStatus();
				$auth = Zend_Auth::getInstance();
				
		        // Set up the authentication adapter
		        $authAdapter = new Ext_Generic_Authentication( 
		        	$userForm->getValue('username')
		        	, $userForm->getValue('password')
					, $userForm->getValue( 'applicationId')
					, Zend_Db_Table_Abstract::getDefaultAdapter()
		        );
		        
		        $authAdapter->setOwner( 
		        	Zend_Registry::get('config')->user->login->ownerId 
		        );
		        
		        // Attempt authentication, saving the result
		        $result = $auth->authenticate( $authAdapter );
		        if ( $result->isValid() ) {
		        	
		            $this->_user = $auth->getIdentity();

		            $var = setcookie(
						Zend_Registry::get('config')->cookie->token->name,
						$loginStatus->getToken( $this->_user->id ),
			            $loginStatus->getTimeLife(),
			            '/',
			            Zend_Registry::get('config')->site->domain,
			            false
					);
					
					$row = $loginStatus->getRow( 
						$this->_user->id 
					);

					$row->user_id = $this->_user->id;

					$row->token = $loginStatus->getToken( $this->_user->id );
		            $row->is_login = 'T';
					//$row->last_login = Ext_Db_Date::now();
					//$row->expiration_date = new Ext_Db_Date(
						//Ext_Db_Date::now()->addDay( 6 ));
					$row->save();
					$this->user->token = $row->token;
			        Zend_Registry::set( 'user', $this->_user );
			        if( $this->getRequest()->url_response ) {
						$url = $this->getRequest()->url_response . "?token=" . $this->user->token;
  						return $this->_redirect( $url );
			        } else {
			        	return $this->_redirect( $this->_config->login->success->url );
			        }
		        } else {
		        	if( $this->getRequest()->url_response ) {
						$url = $this->getRequest()->url_response . "?token=&error=1";
  						return $this->_redirect( $url );
			        } else {
			        	$this->addError( 'El formulario contiene errores' );
			        }
	            }
            }
	    } else {
        	$userForm->applicationId->setValue( 
	        	Zend_Registry::get('config')->main->app->id 
    	    );
	    }
	    $this->view->userForm = $userForm;
	}

	public function logoutAction()
	{
		$auth = Zend_Auth::getInstance();
		if ( $auth->hasIdentity() ) {
            $this->_user = $auth->getIdentity();
			$loginStatus = new SsoPanel_Model_UserLoginStatus ();
			$row = $loginStatus->getRow( $this->_user->id );
			//print_r($this->_user->id);exit;
			$row->is_login = 'F';
			$row->delete();
		}
		parent::logoutAction();
	}
}
